package com.wbt.bambootown.ui.adapter;


import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.Facility;
import com.wbt.bambootown.entity.Farms;
import com.wbt.bambootown.ui.view.recyclerview.BaseAdapter;
import com.wbt.bambootown.ui.view.recyclerview.BaseViewHolder;
import com.wbt.bambootown.utils.SPUtils;
import com.zhy.autolayout.utils.AutoUtils;


/**
 * Created by wufan on 2018/5/16.
 */

public class FacilityAdapter extends BaseAdapter<Facility, BaseViewHolder>{

    private Context context;

    public FacilityAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        TextView textView = new TextView(context);
        textView.setBackgroundResource(R.drawable.border_solid_oval);
        textView.setGravity(Gravity.CENTER);
        return new BaseViewHolder(parent , textView);
    }

    @Override
    public void bindCustomViewHolder(final BaseViewHolder viewHolder, final int position) {
        AutoUtils.autoSize(viewHolder.itemView);

        final Facility item = getItem(position);
        if(SPUtils.getInt(Cons.SPCons.FACILITY_CHOOSE_NUM , 0) == position){
            ((TextView)viewHolder.itemView).setTextColor(context.getResources().getColor(R.color.text_yellow));
        }else{
            ((TextView)viewHolder.itemView).setTextColor(context.getResources().getColor(R.color.text_color_mid));
        }
        ((TextView)viewHolder.itemView).setText(item.getName());

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(SPUtils.getInt(Cons.SPCons.FACILITY_CHOOSE_NUM) != position){
                    SPUtils.putInt(Cons.SPCons.FACILITY_CHOOSE_NUM , position);
                    ((TextView)viewHolder.itemView).setTextColor(context.getResources().getColor(R.color.text_yellow));
                }
                mOnItemClick.chooseFacility(item.getId() , position);
            }
        });
    }

    @Override
    public int getCustomViewType(int position) {
        return 0;
    }

    public onItemClick mOnItemClick;

    public interface onItemClick {
        public void chooseFacility(int id , int position);
    }

    public void setListener(onItemClick mListener) {
        this.mOnItemClick = mListener;
    }
}
